package com.clubs.coding.Service.Impl;

import com.clubs.coding.Service.UserService;
import com.clubs.coding.exception.ImoocMallException;
import com.clubs.coding.exception.ImoocMallExceptionEnum;
import com.clubs.coding.model.Vo.UserVo;
import com.clubs.coding.model.dao.AthCheckMapper;
import com.clubs.coding.model.dao.UserMapper;
import com.clubs.coding.model.pojo.AthCheck;
import com.clubs.coding.model.pojo.User;
import com.clubs.coding.model.request.UserReq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    AthCheckMapper athCheckMapper;

    @Override
    public User findByOpenid(String openid){
        User user = userMapper.findByOpenid(openid);
    return user;
    }
    @Override
    public void inopenid(User user){
       int i = userMapper.insertSelective(user);

    }
    @Override
    public void updateuser(User user){
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i ==0) {
            throw new  ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }

    }
    @Override
    public UserVo userinfo(String openid){
        UserVo userVo = new UserVo();
        User user = userMapper.selectUserInfo(openid);
        List<AthCheck> athChecks = athCheckMapper.selectByAll();
        for (int i = 0; i < athChecks.size(); i++) {
            AthCheck athCheck =  athChecks.get(i);
            if (user.getId().equals(athCheck.getUserId()) && athCheck.getUpdateTime()!=null) {
                userVo.setAthCheckId("1");
            }else {
                userVo.setAthCheckId("0");
            }

        }
        if (user==null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.OPENID_NO);
        }


        userVo.setAvatarUrl(user.getAvatarUrl());
        userVo.setMobile(user.getMobile());
        userVo.setNickName(user.getNickName());
        userVo.setIsMember(user.getIsMember());
        return userVo;
    }
    @Override
public void ipi(UserReq userReq){
        User user1 = userMapper.findByOpenid(userReq.getOpenId());

    BeanUtils.copyProperties(userReq,user1);
        int i = userMapper.updateByPrimaryKeySelective(user1);
        if (i==0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR);
        }

    }
    //开通会员
    @Override
    public void openMembr(String openid){
        User user = userMapper.selectUserInfo(openid);
        if (user ==null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.OPENID_NO);
        }
//        开通后写入审核表
        AthCheck athCheck = new AthCheck();
        athCheck.setUserId(user.getId());
        athCheck.setCheckMoblie(user.getMobile());
        athCheck.setCheckName(user.getNickName());
        athCheck.setCreatTime(new Date());
        athCheckMapper.insertSelective(athCheck);
    }
    //换取手机号
    @Override
    public String swap(String mobile){
        User user = userMapper.selectByPhone(mobile);
        if (user == null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.MOBILE_NO);
        }

        return user.getOpenId();
    }
}
